import java.util.Arrays;
import java.util.Comparator;

public class Leetcode1779 {
    public int nearestValidPoint(int x, int y, int[][] points) {
        int min = Integer.MAX_VALUE;
        int res = -1;
        for (int i = 0; i < points.length; i++) {
            if (points[i][0] == x) {
                int abs = Math.abs(points[i][1] - y);
                if (abs < min) {
                    min = abs;
                    res = i;
                }
            }

            if (points[i][1] == y) {
                int abs = Math.abs(points[i][0] - x);
                if (abs < min) {
                    min = abs;
                    res = i;
                }
            }
        }

        return res;
    }

    /**
     * 一行娱乐玩法
     * @param x
     * @param y
     * @param points
     * @return
     */
    public int nearestValidPoint1(int x, int y, int[][] points) {
        return Arrays.asList(points).indexOf(Arrays.stream(points).filter(p -> p[0] == x || p[1] == y).min(Comparator.comparingInt(p -> Math.abs(p[0] - x) + Math.abs(p[1] - y))).orElse(null));
    }

}
